package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/6/13 21:46
 */
public class C52 {


    boolean[][] used;
    public int totalNQueens(int n) {
        used = new boolean[n][n];
        return process(0, n);
    }

    public int process(int i, int N) {
        if(i == N) return 1;
        int ways = 0;
        for (int c = 0; c < N; c++) {
            boolean can = true;
            for (int k = 0; k < i; k++) {
                int distance = i - k;
                // 如果上边, 或者左上直线, 或者右上直线
                if(used[k][c] || ((c-distance) >= 0 && used[k][c-distance]) || ((c + distance) < N && used[k][c+distance])) {
                    can = false;
                    break;
                }
            }

            if(can) {
                used[i][c] = true;
                ways += process(i+1, N);
                used[i][c] = false;
            }
        }
        return ways;
    }

}
